package com.study.concurrent.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockTest {
    private final static ReadWriteLock lock = new ReentrantReadWriteLock();

    public static void main(String[] args) {
        new Thread(()->{
            try {
                lock.readLock().lock();
                System.out.println("1上读锁");
                TimeUnit.SECONDS.sleep(60
                );
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                System.out.println("1解读锁");
                lock.readLock().unlock();
            }
        }).start();
        new Thread(()->{
            try {
                lock.writeLock().lock();
                System.out.println("2上写锁");
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                System.out.println("2解写锁");
                lock.writeLock().unlock();
            }
        }).start();
//        new Thread(()->{
//            try {
//                lock.readLock().lock();
//                System.out.println("3上读锁");
//                TimeUnit.SECONDS.sleep(1);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }finally {
//                System.out.println("3解读锁");
//                lock.readLock().unlock();
//            }
//        }).start();
    }
}
